package com.example.hand.api.controller.v1;

import com.example.hand.app.service.EmployeeService;
import com.example.hand.domain.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("v1/employee")
public class EmployeeController {

    private final EmployeeService employeeService;

    //构造器注入
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }


    /**
     * 查询全部
     *
     * @param employee
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<?> selectEmployees(Employee employee) {
        log.info("查询全部数据...");
        List<Employee> employeeList = employeeService.selectEmployeeList(employee);
        return ResponseEntity.ok(employeeList);
    }


    /**
     * 通过id查询
     *
     * @param employeeId
     * @return
     */
    @GetMapping("/{employeeId}")
    public ResponseEntity<?> selectEmployeeById(@PathVariable("employeeId") Long employeeId) {
        Employee employee = employeeService.selectEmployeeById(employeeId);
        return ResponseEntity.ok(employee);
    }


    /**
     * 添加
     *
     * @param employee
     * @return
     */
    @PutMapping("/insert")
    public ResponseEntity<?> insertEmployee(@RequestBody Employee employee) {
        Employee addEmployee = employeeService.insertEmployee(employee);
        return ResponseEntity.ok(addEmployee);

    }


    /**
     * 修改
     *
     * @param employee
     * @return
     */
    @PutMapping("/update")
    public ResponseEntity<?> updateEmployee(@RequestBody Employee employee) {
        Employee updateEmployee = employeeService.updateEmployee(employee);
        return ResponseEntity.ok(updateEmployee);

    }

    /**
     * 删除
     *
     * @param employeeId
     * @return
     */
    @DeleteMapping("/delete/{employeeId}")
    public ResponseEntity<?> deleteEmployee(@PathVariable("employeeId") Long employeeId) {
        Employee deleteEmployee = employeeService.deleteEmployee(employeeId);
        return ResponseEntity.ok(deleteEmployee);
    }

    /**
     * 根据empid查询下属员工
     *
     * @param employeeId
     * @return
     */
    @GetMapping("/get-employee-with-leader")
    public ResponseEntity<?> getLeaderByEmployeeId(@RequestBody Employee employeeId) {
        Employee employee = employeeService.getLeaderByEmployeeId(employeeId);
        return ResponseEntity.ok(employee);

    }
}
